import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class TcpClient {
    public static void main(String[] args) {
        for(int i=0;i<100;i++){
            final int finalI = i;
            System.out.println(i+"test ");
            new Thread(()->{
                try {
                    System.out.println("我在运行"+finalI);
                    InetAddress localHost = InetAddress.getByName("127.0.0.1");
                    Socket socket = new Socket(localHost,5678);
                    OutputStream out = socket.getOutputStream();
                    InputStream in = socket.getInputStream();
                    System.out.println("准备开始");
                    out.write("hello,我是客户端".getBytes());
                    out.flush();
                    int len;
                    byte[] b1 = new byte[1024];
                    StringBuilder str = new StringBuilder();
                    while ((len = in.read(b1)) >0 ){
                        String str1 = new String(b1,0,len,"UTF-8");
                        System.out.println(str1);
                        str.append(str1);
                    }
                    System.out.println(str);
                    out.write("我已接受".getBytes());
                    socket.close();
                } catch (IOException e) {
                    System.out.println(finalI);
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
